package cn.bluethink.mouse;

import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JOptionPane;

import cn.bluethink.MapFrame;
import cn.bluethink.graphical.Polyline;
import cn.bluethink.utile.GraphicalUtil;
import cn.bluethink.view.GraphView;

public class MouseDrawPolylineOp implements IMouseOperator {

    private Polyline mPolyline = new Polyline();
    private GraphView mGraphView;

    public MouseDrawPolylineOp(GraphView view) {
        this.mGraphView = view;
    }

    @Override
    public void mouseClick(MouseEvent event) {
    }

    @Override
    public void mouseReleased(MouseEvent event) {
    }

    @Override
    public void mouseEntered(MouseEvent event) {
    }

    @Override
    public void mousePressed(MouseEvent event) {
        //点击鼠标左键向points集合添加点
    	 if (event.getButton() == MouseEvent.BUTTON1) {
             mPolyline.getPoints().add(event.getPoint());
             if (mPolyline.getHandleCount() == 1){
                 mPolyline.getPoints().add(event.getPoint());
             }
         }else if (event.getButton() == MouseEvent.BUTTON3){
    	     //重新绘制另一折线段并自动保存
             Polyline polyline = new Polyline(mPolyline);
             mGraphView.mGraphs.add(polyline);
             mPolyline.getPoints().clear();
             mGraphView.repaint();
             try {
				if (GraphicalUtil.saveGraphInfo("save",null,MapFrame.canvas.mGraphs.get(0)) == null) {
					JOptionPane.showMessageDialog(null, "自动保存折线失败，请你先登录", "出错啦", JOptionPane.ERROR_MESSAGE);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
         }
    }

    @Override
    public void mouseExited(MouseEvent event) {
    }

    @Override
    public void mouseDragged(MouseEvent event) {
    }

    @Override
    public void mouseMoved(MouseEvent event) {
        if (mPolyline.getHandleCount() == 0) {
            return;
        }
        mPolyline.draw(mGraphView.getGraphics());
        mPolyline.getLastPoint().setLocation(event.getPoint());
        mGraphView.refresh();
        mPolyline.draw(mGraphView.getGraphics());
    }
}
